package com.sikao.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.EntrustListBean;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.utils.TimeUtil;

import java.util.List;

/**
 * Created by y50 on 2016/5/16.
 */
public class MyEntrustsAdapter extends BaseAdapter {

	private Context context;
	private List<EntrustListBean.ResBean.ListBean> beans;
	private Callback cancelAttention;

	public MyEntrustsAdapter(Context context, List<EntrustListBean.ResBean.ListBean> beans, Callback cancelAttention) {
		this.context = context;
		this.beans = beans;
		this.cancelAttention = cancelAttention;
	}
	@Override
	public int getCount() {
		return beans.size();
	}

	@Override
	public Object getItem(int position) {
		return beans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder holder;
		if (convertView == null){
			view = View.inflate(context, R.layout.my_entrusts_list_item,null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		}else{
			view = convertView;
			holder = (ViewHolder) view.getTag();
		}
		holder.tvEntrustTitle.setText(beans.get(position).getName());
		holder.tvEntrustContent.setText(beans.get(position).getCont());
		holder.tvEntrustTime.setText(TimeUtil.timeStampToTime(beans.get(position).getAdd_time(),TimeUtil.fotmatString2));
		holder.btnDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancelAttention.notice(position);
			}
		});
		return view;
	}

	public class ViewHolder {
		private TextView tvEntrustTitle;
		private TextView tvEntrustTime;
		private TextView tvEntrustContent;
		private Button btnDelete;
		public final View root;

		public ViewHolder(View root) {
			tvEntrustTitle = (TextView) root.findViewById(R.id.tv_entrust_title);
			tvEntrustTime = (TextView) root.findViewById(R.id.tv_entrust_time);
			tvEntrustContent = (TextView) root.findViewById(R.id.tv_entrust_content);
			btnDelete = (Button) root.findViewById(R.id.btn_delete);
			this.root = root;
		}
	}
}
